package com.clz.servlet;

import com.clz.entiy.User;
import com.clz.servise.Impl.UserServiceImpl;
import com.clz.servise.UserService;
import com.jspsmart.upload.Request;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/AddUser")
public class AddUserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      doPost(req,resp );
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        SmartUpload sm=new SmartUpload();
        sm.initialize(this.getServletConfig(),request,response);
        sm.setCharset("UTF-8");
        try {
            sm.upload();
            sm.save("/pict");

        } catch (SmartUploadException e) {
            throw new RuntimeException(e);
        }

        Request req=sm.getRequest();
        String username= req.getParameter("userName");
        String password= req.getParameter("password");
        String pic= sm.getFiles().getFile(0).getFileName();
        User user=new User(null,username,password,pic);

        UserService userService=new UserServiceImpl();
        userService.insertUser(user);
        response.sendRedirect("PageServlet");

    }
}
